GM.Calculator = GM.Calculator or {};
GM.Calculator.Frame = GM_CalculatorForm;
GM.Calculator.Display = GM_CalculatorForm_Label_Result_String;

GM.Calculator.Result = nil;
GM.Calculator.preInput = nil;
GM.Calculator.curInput = nil;
GM.Calculator.curOperate = nil;

-- 关闭计算器
GM.Calculator.Close = function() GM.Calculator.Frame:Hide(); end;

-- 输入
GM.Calculator.Input = function(self, arg1)
    local bOperate = self:IsOperate(arg1);

    -- self.curInput = self.curInput or "";
    if not bOperate then
        self.curInput = self.curInput or "";
	-- 输入小数点, 并且当前输入串中已包含小数点
	if self:IsDot(arg1) and self:HasDot(self.curInput) then
	    return;
	else
	    self.curInput = self.curInput .. arg1;
	    self.Display:SetText(self.curInput);
	end
    elseif bOperate and self.curInput and not self.preInput then
	self.curOperate = arg1;
        self.preInput = self.curInput;
        self.curInput = nil;
    elseif bOperate and not self.curInput and self.preInput then
	self.curOperate = arg1;
	self.Display:SetText(arg1);
    elseif bOperate and self.curInput and self.preInput then
	self:DoResult();
	self:Output();
	self.curOperate = arg1;
    end
end;

GM.Calculator.IsOperate = function(self, arg1)
    if arg1 == "+" or arg1 == "-" or arg1 == "*" or arg1 == "/" or arg1 == "^" or arg1 == "=" then
        return true;
    else
        return false;
    end
end

GM.Calculator.HasDot = function(self, arg1)
    return string.find(arg1, "[.]") ~= nil;
end
GM.Calculator.IsDot = function(self, arg1)
    if arg1 == "." then
        return true;
    else
        return false;
    end
end

-- 计算
GM.Calculator.DoResult = function(self)
    if self.curOperate == "+" then
        self:Add();
    elseif self.curOperate == "-" then
        self:Sub();
    elseif self.curOperate == "*" then
        self:Mul();
    elseif self.curOperate == "/" then
        self:Div();
    elseif self.curOperate == "^" then
        self:Pow();
    elseif self.curOperate == "=" then
        self:Eq();
    else
        print("error DoResult[" .. self.curOperate .. "]");
    end
end
-- 加
GM.Calculator.Add = function(self)
    self.preInput = self.preInput + self.curInput;
    self.Result = self.preInput;
    self.curInput = nil;
end
-- 减
GM.Calculator.Sub = function(self)
    self.preInput = self.preInput - self.curInput;
    self.Result = self.preInput;
    self.curInput = nil;
end
-- 乘
GM.Calculator.Mul = function(self)
    self.preInput = self.preInput * self.curInput;
    self.Result = self.preInput;
    self.curInput = nil;
end
-- 除
GM.Calculator.Div = function(self)
    self.preInput = self.preInput / self.curInput;
    self.Result = self.preInput;
    self.curInput = nil;
end
-- 幂
GM.Calculator.Pow = function(self)
    self.preInput = self.preInput ^ self.curInput;
    self.Result = self.preInput;
    self.curInput = nil;
end
-- 等于
GM.Calculator.Eq = function(self)
    self.preInput = self.curInput;
    self.Result = self.preInput;
    self.curInput = nil;
end

-- 输出
GM.Calculator.Output = function(self)
    self.Display:SetText(self.Result);
end;
